auto.waitFor();
let oldTime = new Date().getTime();
const appName = "抖音极速版";
let searchIsComplete = false;
let watchADIsComplete = false;
let shopIsComplete = false;
launchApp(appName);
sleep(2500);
printTimeGap();
closeFindFrendWindow();
goTask();
console.info(appName);
sleep(1000);
back();
// if (back()) {
sleep(2000);
if (!searchIsComplete) {
  goSearch();
}
slideScreen([5, 14], 300);
// }

// 控件中心坐标点击（很多控件clickable都为false，不能直接触发点击）
function centerClick(control) {
  if (control.exists()) {
    printTimeGap();
    console.info("开始点击" + control.findOnce());
    control.findOnce().clickCenter();
    console.info("点击结束");
    sleep(1000);
  }
}
// 随机上下滑动坐标
function getRandomPosition() {
  return {
    x1: device.width * (random(4, 6) / 10),
    x2: device.width * (random(4, 6) / 10),
    y1: device.height * (random(7, 8) / 10),
    y2: device.height * (random(3, 4) / 10),
  };
}

/*
 gap:滑动间隔(随机范围)
 duration:总时长(秒为单位)
 type:类型（分为时间(0)和次数(1)两种，不传为0）
*/
// 滑动切换视频
function slideScreen(gap, count, type) {
  const { x1, y1, x2, y2 } = getRandomPosition();
  let randomGap;
  if (type === 1) {
    for (let i = 0; i < count; i++) {
      randomGap = random(gap[0], gap[1]);
      swipe(x1, y1, x2, y2, 300);
      sleep(randomGap * 1000);
    }
  } else {
    let timeCount = 0;
    while (timeCount < count) {
      if (swipe(x1, y1, x2, y2, 300)) {
        // console.info("滑动结束");
        randomGap = random(gap[0], gap[1]);
        sleep(randomGap * 1000);
        timeCount += randomGap;
        console.log(timeCount);
      } else {
        console.info("滑动失败");
      }
    }
  }
}

// 进入任务中心
function goTask() {
  const task = className("android.widget.RelativeLayout").depth(5);
  printTimeGap();
  console.log("获取任务按钮");
  task.waitFor();
  printTimeGap();
  console.log("准备点击按钮");
  centerClick(task);
  sleep(6000);
  signIn();
  searchIsComplete = className("com.lynx.tasm.behavior.ui.text.FlattenUIText")
    .text("已获得搜索金币")
    .exists();
  watchAD();
  watchButtomAD();
  shop();
}

// 签到
function signIn() {
  const signInButton = className(
    "com.lynx.tasm.behavior.ui.text.FlattenUIText"
  ).descContains("立即签到");
  centerClick(signInButton);
  keepWatchAD();
}

// 看广告-通过广告任务
function watchAD() {
  const adButton = className("com.lynx.tasm.behavior.ui.LynxFlattenUI")
    .depth(18)
    .text("+10000金币");
  adButton.waitFor();
  centerClick(adButton);
  if (!adButton.exists()) {
    sleep(35000);
    closeAD();
  }
}

// 关闭广告
function closeAD() {
  printTimeGap();
  console.log("开始点击关闭广告");
  coordinateClick(958, 74, 1006, 122);
  sleep(2000);
  printTimeGap();
  const keepButton = className(
    "com.lynx.tasm.behavior.ui.text.UIText"
  ).descContains("再看一个获取");
  const anotherBtn = className("com.lynx.tasm.behavior.ui.view.UIView").text(
    "领取奖励"
  );
  if (keepButton.exists() || anotherBtn.exists()) {
    sleep(1000);
    console.log("开始继续看广告");
    centerClick(keepButton.exists() ? keepButton : anotherBtn);
    sleep(35000);
    closeAD();
  } else {
    // watchButtomAD()
  }
}

// 坐标点击
function coordinateClick(x1, y1, x2, y2) {
  const x = x1 + (x2 - x1) / 2;
  const y = y1 + (y2 - y1) / 2;
  setScreenMetrics(1080, 2400);
  click(x, y);
  sleep(1000);
}

// 看底部广告
function watchButtomAD() {
  console.log("开始查找点击领金币");
  const button = className("com.lynx.tasm.behavior.ui.LynxFlattenUI").desc(
    "点击领金币"
  );
  if (button.exists()) {
    console.log("点击领金币");
    centerClick(button);
    keepWatchAD();
  }
}

// 看广告视频再赚
function keepWatchAD() {
  const watchADButton = className(
    "com.lynx.tasm.behavior.ui.text.FlattenUIText"
  ).desc("看广告视频再赚");
  if (watchADButton.exists()) {
    centerClick(watchADButton);
    sleep(35000);
    closeAD();
  }
}

// 判断是否有发现通讯好友弹窗并关闭
function closeFindFrendWindow() {
  if (id("au").exists()) {
    id("tv_cancel").click();
  }
}

// 打印时间间隔
function printTimeGap() {
  const newTime = new Date().getTime();
  console.log("时间间隔：" + (newTime - oldTime));
  oldTime = newTime;
}

// 逛街赚金币
function shop() {
  const countdown = className("com.lynx.tasm.behavior.ui.LynxFlattenUI")
    .depth(18)
    .textContains("后浏览还可得金币");
  if (countdown.exists()) {
    return;
  }
  console.log("查找浏览低价商品90秒");
  const textButton = className(
    "com.lynx.tasm.behavior.ui.LynxFlattenUI"
  ).textContains("浏览低价商品90秒");
  if (textButton.exists()) {
    console.log("找到浏览低价商品90秒");
    slideFindBtn("com.lynx.tasm.behavior.ui.LynxFlattenUI", "浏览低价商品90秒");
    centerClick(textButton);
    slideScreen([5, 8], 90);
    back();
    sleep(1000);
  }
}

// 滑动找按钮
function slideFindBtn(classString, text) {
  const deviceHeight = device.height;
  const { x1, y1, x2, y2 } = getRandomPosition();
  let bottom = className(classString)
    .textContains(text)
    .findOnce()
    .bounds().bottom;
  while (deviceHeight < bottom) {
    console.log(bottom);
    swipe(x1, y1, x2, y2, 300);
    sleep(1500);
    bottom = className(classString)
      .textContains(text)
      .findOnce()
      .bounds().bottom;
  }
}

// 搜索赚金币
function goSearch() {
  const searchBtn = className("android.widget.Button").desc("搜索");
  const searchTexts = ["hi", "好久不见", "你好吗", "请多指教"];
  if (searchBtn.exists()) {
    centerClick(searchBtn);
    for (let i = 0; i < 4; i++) {
      if (setText(searchTexts[i])) {
        sleep(500);
        centerClick(id("tv_search_textview"));
        slideScreen([1, 3], 15);
      } else {
        console.log("设置失败");
      }
    }
  }

  back();
}

// 判断次数，是否完成
function getIsComplete(str) {
  let res = str.match(/\d+\/\d+/g);
  if (res) {
    res = res.split("/");
    return res[0] === res[1];
  }
  return false;
}
